package cn.mw.cmdb.entity.graph;

import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Data
public class EdgeParam {
    public static final String SEP = "_";
    private String source;
    private String target;
    private String key;

    private String sourceModelId;
    private String sourceInstanceId;

    private String targetModelId;
    private String targetInstanceId;
    private Boolean isEmptyLine;


    public EdgeParam() {
    }

    public EdgeParam(String source, String target) {
        this.source = source;
        this.target = target;
        setKey(source, target);
    }

    public EdgeParam(NodeParam src , NodeParam dest){
        this.source = src.getId();
        this.target = dest.getId();
        setKey(src.getId() ,dest.getId());
    }

    public void setKey(String srcId ,String destId) {
        List<String> list = new ArrayList<>();
        list.add(srcId);
        list.add(destId);
        Collections.sort(list);
        this.key = list.toString();

        String[] srcValues = srcId.split(SEP);
        this.sourceModelId = srcValues[0];
        this.sourceInstanceId = srcValues[1];

        String[] destValues = destId.split(SEP);
        this.targetModelId = destValues[0];
        this.targetInstanceId = destValues[1];
    }

    public List<String> findTargetIntFormat() {
        List<String> list = new ArrayList<>();
        list.add(targetModelId);
        list.add(targetInstanceId);
        return list;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        EdgeParam edgeParam = (EdgeParam) o;
        return Objects.equals(source, edgeParam.source) &&
                Objects.equals(target, edgeParam.target);
    }

    @Override
    public int hashCode() {
        return Objects.hash(source, target);
    }
}
